/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AccesoDatos;

import java.sql.*;
import java.util.logging.*;


/**
 *
 * @author dani
 */
public class ConMySQL {
    private String Usuario="fincafe";
    private String Pass="cafejava";
    private String Host="localhost";
    private String DB="fincafe";
    private String Puerto="3306";
    private String CadenaConn="jdbc:mysql://";
    private Connection conn=null;
    private ResultSet res;
    
    public ConMySQL() {
        this.CadenaConn+=this.Host+":"+this.Puerto+"/"+this.DB;
    }
    
    public ConMySQL(String host, String usuario, String pass, String db, String puerto) {
        this.Host=host;
        this.Usuario=usuario;
        this.Pass=pass;
        this.DB=db;
        this.Puerto=puerto;
        this.CadenaConn+=this.Host+":"+this.Puerto+"/"+this.DB;
    }    
    
    public int Agregar(Dato[] c, String tabla) {
        try {
            String prepStatement = "insert into " + tabla;
            prepStatement += "(";
            int con = 0;
            for (int i = 0; i < c.length; i++) {
                con++;
                prepStatement += c[i].getNombre();
                if (con < c.length) {
                    prepStatement += ",";
                }            
            }
            
            prepStatement += ") values(";
            
            con = 0;
            for (int i = 0; i < c.length; i++) {
                con++;
                prepStatement += "'"+c[i].getValor()+"'";
                if (con < c.length) {
                    prepStatement += ",";
                }
            }
            
            prepStatement+=")";
            
            return this.conn.createStatement().executeUpdate(prepStatement);
            
        } catch (SQLException ex) {
            Logger.getLogger(ConMySQL.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }
    
    public int Agregar(String[] valor, String tabla) {
        try {
            String prepStatement = "insert into " + tabla+"() ";
            prepStatement += "values(";
            int con = 0;
            for (int i = 0; i < valor.length; i++) {
                con++;
                prepStatement += "'"+valor[i]+"'";
                if (con < valor.length) {
                    prepStatement += ",";
                }
            }
            prepStatement += ")";
            return this.conn.createStatement().executeUpdate(prepStatement);
        } catch (SQLException ex) {
            Logger.getLogger(ConMySQL.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }
    
    public int ejecutarActualizacion(String actualizacionSQL) {
        try {
            return this.conn.createStatement().executeUpdate(actualizacionSQL);
        } catch (SQLException ex) {
            Logger.getLogger(ConMySQL.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
        
    }
    
    public Vista crearVista(String sqlVista) {
        try {
            this.res = this.conn.createStatement().executeQuery(sqlVista);
            return new Vista(this.res);
        } catch (SQLException ex) {
            Logger.getLogger(ConMySQL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public Boolean Conectar() {
        try {            
            
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            
            conn = DriverManager.getConnection(this.CadenaConn, this.Usuario, this.Pass);
            if (conn!=null)            
                return true;            
            else
                return false;
            
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return false;
        }
    }
    
    public void Cerrar() {
        try {
            this.conn.close();
        } catch (SQLException ex) {
            System.out.println(ex.toString());
        }
    }
}
